package com.yami.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.ProdExtension;
import com.yami.shop.bean.model.SkuStockLock;
import com.yami.shop.common.constants.CacheNames;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.dao.ProdExtensionMapper;
import com.yami.shop.service.ProdExtensionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 *
 *
 * @author LGH
 * @date 2022-05-05 10:47:48
 */
@Service
public class ProdExtensionServiceImpl extends ServiceImpl<ProdExtensionMapper, ProdExtension> implements ProdExtensionService {

    @Autowired
    private ProdExtensionMapper prodExtensionMapper;


    @Override
    @Cacheable(cacheNames = CacheNames.PROD_EXTENSION_INFO_KEY, key = "#prodId")
    public ProdExtension getByProdId(Long prodId) {
        ProdExtension prodExtension = getOne(new LambdaQueryWrapper<ProdExtension>().eq(ProdExtension::getProdId, prodId));
        if (Objects.isNull(prodExtension)) {
            throw new YamiShopBindException("yami.product.no.exist");
        }
        return prodExtension;
    }

    @Override
    public int updateProdStocks(Long prodId) {
        return prodExtensionMapper.updateProdStocks(prodId);
    }

    @Override
    public int updateWaterSoldNum(Integer waterSaleNum, Long prodId) {
        return prodExtensionMapper.updateWaterSoldNum(waterSaleNum, prodId);
    }

    @Override
    @CacheEvict(cacheNames = CacheNames.PROD_EXTENSION_INFO_KEY, key = "#prodId")
    public void removeProdExtensionCache(Long prodId) {

    }

    @Override
    public void reduceActualStockByOrder(List<SkuStockLock> skuWithStocks) {
        prodExtensionMapper.reduceActualStockByOrder(skuWithStocks);
    }

    @Override
    public int updateProdStocksAndLockStock(Long prodId) {
        return prodExtensionMapper.updateProdStocksAndLockStock(prodId);
    }
}
